package nicewarm.com.airbansearchanimation;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import nicewarm.com.airbansearchanimation.adapter.TabViewPageAdapter;
import nicewarm.com.airbansearchanimation.fragment.FirstPageFragment;
import nicewarm.com.airbansearchanimation.fragment.SecondPageFragment;

public class MainActivity extends AppCompatActivity implements FirstPageFragment.AlphaTabBarListener {
    private static final String Tag = MainActivity.class.getSimpleName();

    private TabLayout mTabs;
    private ViewPager mViewPager;
    private int mAlpha;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        mTabs = (TabLayout) findViewById(R.id.tabs);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mTabs.setTranslationY(Utils.getStatusBarHeight(this));
        mTabs.setBackgroundColor(getResources().getColor(R.color.trans));/*getBackground().setAlpha(0);*/
        initViewPager();
    }


    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new FirstPageFragment());
        fragments.add(new SecondPageFragment());
        fragments.add(new SecondPageFragment());
        fragments.add(new SecondPageFragment());

        List<String> titles = new ArrayList<>();
        titles.add(getString(R.string.tab_first));
        titles.add(getString(R.string.tab_second));
        titles.add(getString(R.string.tab_third));
        titles.add(getString(R.string.tab_fourth));

        mTabs.addTab(mTabs.newTab().setText(titles.get(0)));
        mTabs.addTab(mTabs.newTab().setText(titles.get(1)));
        mTabs.addTab(mTabs.newTab().setText(titles.get(2)));
        TabViewPageAdapter mAdapter = new TabViewPageAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mViewPager);
        mTabs.setTabMode(TabLayout.MODE_FIXED);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    if (mAlpha == 0) {
                        mTabs.setBackgroundColor(getResources().getColor(R.color.trans));
                        mTabs.setTabTextColors(getResources().getColor(R.color.white_normal), getResources().getColor(R.color.white));
                    } else {
                        mTabs.setBackgroundColor(Color.argb((int) mAlpha, 255, 255, 255));
                        mTabs.setTabTextColors(Color.argb((int) mAlpha, 0, 0, 0), Color.argb((int) mAlpha, 22, 160, 239));
                    }

                } else {
                    mTabs.setBackgroundColor(getResources().getColor(R.color.white));
                    mTabs.setTabTextColors(getResources().getColor(R.color.black), getResources().getColor(R.color.black_sel));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void alphaTabBar(int totalDy, int itemheight) {
        double progress = (double) totalDy / itemheight;
        Log.e(Tag, "progress : " + progress);
        double alpha = 255 * progress;
        Log.e(Tag, "alpha : " + alpha);
        mAlpha = (int) alpha;
        if (mAlpha > 255) {
            mAlpha = 255;
        }
        mTabs.setTabTextColors(Color.argb((int) mAlpha, 0, 0, 0), Color.argb((int) mAlpha, 22, 160, 239));
        if (mAlpha == 0) {
            mTabs.setTabTextColors(getResources().getColor(R.color.white_normal), getResources().getColor(R.color.white));
        }
        mTabs.setBackgroundColor(Color.argb((int) mAlpha, 255, 255, 255));
    }
}
